package cn.com.service.user.ctrl;

import cn.dev33.satoken.stp.StpUtil;
import cn.com.service.user.service.IUserBaseService;
import cn.qitom.service.user.entity.UserBase;
import cn.qitom.starter.redis.LegoRedis;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * 控制器
 *
 * @author
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserBaseController {

	private final IUserBaseService userBaseService;
	private final LegoRedis legoRedis;
	
	
	
	// 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
	@RequestMapping("doLogin")
	public Object doLogin(String username, String password) {

		Long momo = legoRedis.incr("momo", 2);
		UserBase one = userBaseService.getOne(new QueryWrapper<>(new UserBase()).lambda()
				 .eq(UserBase::getPhone, username).eq(UserBase::getPassword, password));
		Page page = userBaseService.page(new Page());
		if(one!=null){
			StpUtil.setLoginId(one.getId());
			return StpUtil.getTokenInfo();
		}
		HashMap map = new HashMap(){{
			put("error_code", 400);
			put("error_description", "用户名或密码不正确");
		}};
		return map;
	}

	// 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
	@RequestMapping("isLogin")
	public String isLogin(String username, String password) {
		return "当前会话是否登录：" + StpUtil.isLogin();
	}


}
